//! HTTP API服务模块
//! 
//! 提供RESTful API接口，用于沙箱管理和操作

pub mod handlers;
pub mod models;
pub mod client;
pub mod grpc_client;

use crate::{Result, Error};
use actix_web::{web, App, HttpServer, middleware::Logger};
use std::sync::Arc;

/// API服务器
pub struct ApiServer {
    port: u16,
    host: String,
}

impl ApiServer {
    /// 创建新的API服务器
    pub fn new(host: String, port: u16) -> Self {
        Self { host, port }
    }
    
    /// 启动API服务器
    pub async fn start(&self) -> Result<()> {
        tracing::info!("Starting API server on {}:{}", self.host, self.port);
        
        HttpServer::new(|| {
            App::new()
                .wrap(Logger::default())
                .route("/health", web::get().to(handlers::health_check))
                .route("/api/v1/sandboxes", web::post().to(handlers::create_sandbox))
                .route("/api/v1/sandboxes/{id}", web::get().to(handlers::get_sandbox))
                .route("/api/v1/sandboxes/{id}", web::delete().to(handlers::destroy_sandbox))
                .route("/api/v1/sandboxes/{id}/execute", web::post().to(handlers::execute_code))
                .route("/api/v1/sandboxes/{id}/status", web::get().to(handlers::get_sandbox_status))
                .route("/api/v1/stats", web::get().to(handlers::get_stats))
                .route("/metrics", web::get().to(handlers::get_metrics))
        })
        .bind(format!("{}:{}", self.host, self.port))?
        .run()
        .await?;
        
        Ok(())
    }
}

impl Default for ApiServer {
    fn default() -> Self {
        Self::new("127.0.0.1".to_string(), 8080)
    }
}